/*
 * Copyright (C) 2010 恒康信息科技有限公司
 * 版权所有
 */
package com.jjmmbb.kebiao.comm;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jjmmbb.kebiao.R;
import com.jjmmbb.kebiao.db.CourseDao;
import com.jjmmbb.kebiao.pojo.Course;

/**
 * 通用对话框<br/>
 * 最多支持3个按钮,第3个按钮默认隐藏<br/>
 * 
 * @author yizhe
 * @date 2012-6-5
 */
public class InfoDialog extends Dialog implements OnClickListener {
	Context context;
	Course c;
	TextView cellView;
	TextView titleView;
	EditText nameView, teacherView, memoView;
	public Button bt_1, bt_2;

	String title;

	boolean is1Bt = false; // 是否只要1个按钮

	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.PROGRESS_VISIBILITY_ON);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);

		titleView = (TextView) findViewById(R.id.info_title);
		bt_1 = (Button) findViewById(R.id.info_bt1);
		bt_2 = (Button) findViewById(R.id.info_bt2);
		nameView = (EditText) findViewById(R.id.info_name);
		teacherView = (EditText) findViewById(R.id.info_teacher);
		memoView = (EditText) findViewById(R.id.info_memo);

		bt_1.setOnClickListener(this);
		bt_2.setOnClickListener(this);

		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数
		if (null != p) {
			p.width = Util.dip2px(context, 200);
		}

		ini();
	}

	void ini() {
		nameView.setText(c.name);
		teacherView.setText(c.teacher);
		memoView.setText(c.meno);

		if (!Comm.isEdit) {
			nameView.setEnabled(false);
			nameView.setFocusable(false);
			teacherView.setEnabled(false);
			teacherView.setFocusable(false);
			memoView.setEnabled(false);
			memoView.setFocusable(false);
		}
		StringBuffer title = new StringBuffer();
		switch (c.x) {
		case 1:
			title.append(context.getString(R.string.main_week1));
			break;
		case 2:
			title.append(context.getString(R.string.main_week2));
			break;
		case 3:
			title.append(context.getString(R.string.main_week3));
			break;
		case 4:
			title.append(context.getString(R.string.main_week4));
			break;
		case 5:
			title.append(context.getString(R.string.main_week5));
			break;
		case 6:
			title.append(context.getString(R.string.main_week6));
			break;
		case 7:
			title.append(context.getString(R.string.main_week7));
			break;
		}
		title.append(" ");
		if (c.y < 10) {
			title.append(context.getString(R.string.des_am));
		} else {
			title.append(context.getString(R.string.des_pm));
		}
		title.append(" ");
		title.append("第" + (c.y % 10 + 1) + "节");

		titleView.setText(title.toString());
	}

	/**
	 * 创建对话框,使用默认标题 R.string.dialog_defaultTitle
	 * 
	 * @param context
	 * @param msg
	 *            消息
	 */
	public InfoDialog(Context context, Course c, TextView v) {
		super(context);
		this.context = context;
		this.c = c;
		this.cellView = v;
	}

	/**
	 * 设置按钮的文本
	 * 
	 * @param index
	 *            按钮序号
	 * @param text
	 *            按钮文本,为null或者""时不修改
	 */
	public void setButtonText(int index, String text) {
		if (Util.isNull(text)) {
			return;
		}
		switch (index) {
		case 1:
			bt_1.setText(text);
			break;
		case 2:
			bt_2.setText(text);
			break;

		}
	}

	// --------------------单击事件,用到请重写-------------------//
	protected void bt1Click() {
		this.dismiss();// 不会触发oncancel事件
	}

	protected void bt2Click() {
		this.dismiss();
	}

	// --------------------------on-----------------------
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.info_bt1:
			if (Comm.isEdit) {
				c.name = nameView.getText().toString();
				c.teacher = teacherView.getText().toString();
				c.meno = memoView.getText().toString();
				CourseDao cm = new CourseDao(context);
				cm.update(c);
				cellView.setText(c.name);
			}
			this.dismiss();
			break;
		case R.id.info_bt2:
			this.dismiss();
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 按下键盘上返回按钮
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.dismiss();
		}
		return true;
	}
}
